package logic

import (
	"context"
	"zhihu/models/mysql"

	"zhihu/zhihuRpc/internal/svc"
	"zhihu/zhihuRpc/zhihuRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type QuestionReplyListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQuestionReplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QuestionReplyListLogic {
	return &QuestionReplyListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *QuestionReplyListLogic) QuestionReplyList(in *zhihuRpc.QuestionReplyListRequest) (*zhihuRpc.QuestionReplyListResponse, error) {
	// todo: add your logic here and delete this line

	var replies []mysql.Reply
	mysql.DB.Debug().Where("question_id = ? ", in.QuestionId).Find(&replies)

	var questionReplyList []*zhihuRpc.QuestionReplyList

	for _, reply := range replies {
		list := zhihuRpc.QuestionReplyList{
			Id:         int64(reply.ID),
			Content:    reply.Content,
			UserId:     reply.UserId,
			QuestionId: reply.QuestionId,
		}

		questionReplyList = append(questionReplyList, &list)
	}
	return &zhihuRpc.QuestionReplyListResponse{
		List: questionReplyList,
	}, nil
}
